package com.sunny.ynhz.ui.ynts;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.WxyyDBAdapter;
import com.sunny.ynhz.bean.WxyyBean;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.WxyyModel;
import com.sunny.ynhz.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huhuan123 on 2018/8/11.
 *
 * @describe TODO
 */

public class WxyyFragment extends BaseFragment implements IProblemTypeResultView<Object> {
    public int state = 0;
    private WxyyModel wxyyModel;
    List<WxyyBean.RateListBean> mList = new ArrayList<>();
    @BindView(R.id.rl_wxyy)
    RecyclerView rl_wxyy;
    private WxyyDBAdapter wxyyDBAdapter;
    private String mstrTime = "";
    private String mendTime = "";
    private String mriverName = "";
    private int current = 1;
    private int pageSize = 10;
    private String type;


    public static WxyyFragment getInstanceWxyyFragment() {
        return new WxyyFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_wxyy_lv;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        wxyyModel = new WxyyModel(this, getContext());
        wxyyDBAdapter = new WxyyDBAdapter(mList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_wxyy.setLayoutManager(linearLayoutManager);
        rl_wxyy.setAdapter(wxyyDBAdapter);
        Bundle arguments = getArguments();
        state = arguments.getInt("position");
        type = arguments.getString("type");
        askData();
        wxyyDBAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                askData();

            }
        }, rl_wxyy);
        wxyyDBAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WxyyBean.RateListBean rateListBean = mList.get(position);
                String problemid = rateListBean.getPROBLEMID();
                doActivity(WxyyDetailActivity.class, "id", problemid);
            }
        });

    }

    @Override
    public void onSuccess(Object wxyyBean) {
        loadSuccess();
        if (wxyyBean instanceof WxyyBean) {
            WxyyBean wxyyBean1 = (WxyyBean) wxyyBean;
            mList.addAll(wxyyBean1.getRateList());
            wxyyDBAdapter.notifyDataSetChanged();
            WxyyActivity wxyyActivity = (WxyyActivity) getActivity();
            wxyyActivity.setTextView(wxyyBean1.getDate());
            int total = 0;
            if (state == 0) {
                total = wxyyBean1.getDate().get(0).getPCOUNT();//总数
            } else if (state == 1) {
                total = wxyyBean1.getDate().get(0).getYCOUNT();//已办结
            } else if (state == 2) {
                total = wxyyBean1.getDate().get(0).getWCOUNT();
                } else if (state == 3) {
                total = wxyyBean1.getDate().get(0).getDCOUNT();
            }
            wxyyDBAdapter.loadMoreComplete();
            if (total > mList.size()) {
                current = current + 1;
                wxyyDBAdapter.setEnableLoadMore(true);

            } else {
                wxyyDBAdapter.setEnableLoadMore(false);
                wxyyDBAdapter.loadMoreEnd(true);
            }


        }


    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {
        if (current == 1) {
            loadFail();
        }


    }

    @Override
    public void onStartLoad() {
        if (current == 1) {
            startLoad();
        }


    }

    @Override
    public void resLoad() {
        super.resLoad();
        current = 1;
        askData();
    }

    public void notyData(String startTime, String endTime, String riverName) {
        if (mstrTime.equals(startTime) && mendTime.equals(endTime) && mriverName.equals(riverName)) {
            return;
        }
        mList.clear();
        wxyyDBAdapter.notifyDataSetChanged();
        current = 1;
        mstrTime = startTime;
        mendTime = endTime;
        mriverName = riverName;
        askData();
    }

    private void askData() {
        if (state == 0) {
            wxyyModel.getWxyyModelBean(mstrTime, mendTime, type, "0", mriverName, current, pageSize);//全部
        } else if (state == 1) {
            wxyyModel.getWxyyModelBean(mstrTime, mendTime, type, "1", mriverName, current, pageSize);//已办结
        } else if (state == 2) {
            wxyyModel.getWxyyModelBean(mstrTime, mendTime, type, "3", mriverName, current, pageSize);//处理中
        } else if (state == 3) {
            wxyyModel.getWxyyModelBean(mstrTime, mendTime, type, "2", mriverName, current, pageSize);//待复核
        }
    }
}
